﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using MongoDB.Bson.Serialization.Attributes;



namespace ifunction.Fiddler.Model
{
    /// <summary>
    /// Class RequestPackage.
    /// </summary>
    [DataContract]
    public class RequestPackage
    {
        /// <summary>
        /// Gets or sets the key.
        /// </summary>
        /// <value>The key.</value>
        [DataMember]
        [BsonId]
        public Guid? Key { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the main request raw.
        /// </summary>
        /// <value>The main request raw.</value>
        [DataMember]
        public string MainRequestRaw { get; set; }

        /// <summary>
        /// Gets or sets the keep session request raw.
        /// </summary>
        /// <value>The keep session request raw.</value>
        [DataMember]
        public string KeepSessionRequestRaw { get; set; }

        /// <summary>
        /// Gets or sets the keep session interval.
        /// </summary>
        /// <value>The keep session interval.</value>
        [DataMember]
        public long KeepSessionInterval { get; set; }

        /// <summary>
        /// Gets or sets the schedule.
        /// </summary>
        /// <value>The schedule.</value>
        [DataMember]
        public string Schedule { get; set; }

        #region Xml

        /// <summary>
        /// The root node
        /// </summary>
        protected const string RootNode = "Package";

        /// <summary>
        /// The node_ main
        /// </summary>
        protected const string Node_Main = "Main";

        /// <summary>
        /// The node_ keep alive
        /// </summary>
        protected const string Node_KeepAlive = "KeepAlive";

        /// <summary>
        /// The attribute_ interval
        /// </summary>
        protected const string Attribute_Interval = "Interval";

        /// <summary>
        /// The attribute_ schedule
        /// </summary>
        protected const string Attribute_Schedule = "Schedule";

        /// <summary>
        /// Automatics the XML.
        /// </summary>
        /// <returns>XElement.</returns>
        public XElement ToXml()
        {
            var result = this.CreateXmlNode(RootNode);

            var main = this.CreateXmlNode(Node_Main);
            main.SetValue(this.MainRequestRaw.GetStringValue());
            result.Add(main);

            var keepAlive = this.CreateXmlNode(Node_KeepAlive);
            keepAlive.SetAttributeValue(Attribute_Interval, this.KeepSessionInterval);
            keepAlive.SetValue(this.KeepSessionRequestRaw.GetStringValue());
            result.Add(keepAlive);

            return result;
        }

        /// <summary>
        /// Loads the specified XML string.
        /// </summary>
        /// <param name="xmlString">The XML string.</param>
        /// <returns>RequestPackage.</returns>
        public static RequestPackage Load(string xmlString)
        {
            var element = XElement.Parse(xmlString.GetStringValue());
            if (element != null)
            {
                return Load(element);
            }

            return null;
        }

        /// <summary>
        /// Loads the specified XML.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns>RequestPackage.</returns>
        public static RequestPackage Load(XElement xml)
        {
            RequestPackage result = null;

            if (xml != null && xml.Name.LocalName.Equals(RootNode))
            {
                var main = xml.Element(Node_Main);
                if (main != null)
                {
                    result.MainRequestRaw = main.Value;
                }

                var keepAlive = xml.Element(Node_KeepAlive);
                if (keepAlive != null)
                {
                    result.KeepSessionRequestRaw = keepAlive.Value;
                    result.KeepSessionInterval = keepAlive.GetAttributeValue(Attribute_Interval).ToLong();
                }
            }

            return result;
        }

        #endregion

        #region To RuntimePackage

        /// <summary>
        /// Automatics the runtime package.
        /// </summary>
        /// <returns>RuntimePackage.</returns>
        public RuntimePackage ToRuntimePackage()
        {
            RuntimePackage result = new RuntimePackage();

            result.MainRequest = FiddlerRequest.Create(this.MainRequestRaw);
            result.KeepSessionRequest = FiddlerRequest.Create(this.KeepSessionRequestRaw);
            result.KeepSessionInterval = this.KeepSessionInterval;

            return result;
        }


        #endregion

    }
}
